package com.tanhua.server.service;


import com.tanhua.commons.exception.TanHuaException;
import com.tanhua.domain.Enum.CharacterEnum;
import com.tanhua.domain.Enum.QuestionnaireLeverEnum;
import com.tanhua.domain.dto.Conclusion;
import com.tanhua.domain.dto.OptionsDto;
import com.tanhua.domain.dto.QuestionDto;
import com.tanhua.domain.mongo.*;
import com.tanhua.domain.vo.LHQuestionnaireVo;
import com.tanhua.dubbo.api.mongo.SoulServiceApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//此类用于对灵魂测试的业务处理
@Component
public class SoulService {
@Reference
  private SoulServiceApi soulServiceApi;
   // 类型
    private volatile int type = 0;
   //分数
    private volatile double typeSore = 0;

    //添加问卷
    public String testSoul(List<Map<String, String>> paramMap) {
        LHResult lhResult = new LHResult();

        // 获取第一个问题的objectId
        String questionId = paramMap.get(0).get("questionId");

        // 根据questionId查询问卷
        LHQuestion lhQuestion = soulServiceApi.findQuestionnaireByQuestionId(questionId);
        // 根据问卷获取id
        ObjectId id = lhQuestion.getQuestionnaireId();
        lhResult.setQuestionnaireId(id);
        lhResult.setUserId(UserHolder.getUserId() + "");
        float score1 = 0f;
        float score2 = 0f;
        float score3 = 0f;
        float score4 = 0f;

        int maxScore1 = 0;
        int maxScore2 = 0;
        int maxScore3 = 0;
        int maxScore4 = 0;
        for (Map<String, String> map : paramMap) {
            //通过map获取optionId
            String optionId = map.get("optionId");
            // 通过optionId获取option对象
            LHOptions lhOptions = soulServiceApi.findOptionByOptionId(optionId);
            // 获取分数
            Integer score = lhOptions.getScore();
            // 获取最大分数
            Integer maxScore = lhOptions.getMaxScore();
            // 获取纬度
            Integer dimensions = lhOptions.getDimensions();

            switch (CharacterEnum.getCharacter(dimensions)) {
                case EXTROVERSION:
                    score1 += score;
                    maxScore1 += maxScore;
                    break;
                case VERDICT:
                    score2 += score;
                    maxScore2 += maxScore;
                    break;
                case ABSTRACT:
                    score3 += score;
                    maxScore3 += maxScore;
                    break;
                case IDEALITY:
                    score4 += score;
                    maxScore4 += maxScore;
                    break;
                default:
                    throw new TanHuaException("参数异常");
            }
        }
        LHDimentsions dim1 = new LHDimentsions();
        LHDimentsions dim2 = new LHDimentsions();
        LHDimentsions dim3 = new LHDimentsions();
        LHDimentsions dim4 = new LHDimentsions();
        dim1.setKey(CharacterEnum.EXTROVERSION.getCode());
        dim2.setKey(CharacterEnum.VERDICT.getCode());
        dim3.setKey(CharacterEnum.ABSTRACT.getCode());
        dim4.setKey(CharacterEnum.IDEALITY.getCode());

        String val1 = (score1 / maxScore1) * 100 + "%";
        String val2 = (score2 / maxScore2) * 100 + "%";
        String val3 = (score3 / maxScore3) * 100 + "%";
        String val4 = (score4 / maxScore4) * 100 + "%";

        dim1.setValue(val1);
        dim2.setValue(val2);
        dim3.setValue(val3);
        dim4.setValue(val4);

        List<LHDimentsions> lhDimentsions = new ArrayList<>();
        lhDimentsions.add(dim1);
        lhDimentsions.add(dim2);
        lhDimentsions.add(dim3);
        lhDimentsions.add(dim4);

        lhResult.setDimensions(lhDimentsions);
        Float s = score1 + score2 + score3 + score4;
        int sumScore = s.intValue();
        lhResult.setScore(sumScore);

        Conclusion c = new Conclusion();
        Integer conclusion = c.getConclusionId(sumScore);
        lhResult.setConclusion(conclusion);

        String cover = c.getCover(sumScore);
        lhResult.setCover(cover);

        String resultId = soulServiceApi.addResult(lhResult);
        return resultId;

       /* //获取登录用户id
        Long loginUserId = UserHolder.getUserId();

        //传入答案和问题
        LHOptions lhOptions = new LHOptions();
        //问题
        ObjectId questionId = lhOptions.getQuestionId();
        //答案
        String option = lhOptions.getOption();
        //获取问卷id
        List<LHQuestion> questionList = new ArrayList<>();
        List<ObjectId> questionsId = questionList.stream().map(LHQuestion::getQuestionnaireId).collect(Collectors.toList());
        //通过选项获取最大分
        Integer maxScore = lhOptions.getMaxScore();
        //实际得分
        Integer score = lhOptions.getScore();
        //维度信息
        Integer dimensions = lhOptions.getDimensions();*/

    }
  /**此方法用于查询问卷列表
   * @return 问卷列表
   */

  public List<LHQuestionnaireVo> findAll() {
      //此方法用于判断用户灵魂测试是否加锁了，没有就加要就不管
      soulServiceApi.addLock(UserHolder.getUserId());
      //查询灵魂测试问卷
      List<LHQuestionnaire> test = soulServiceApi.findTest();
      List<LHQuestionnaireVo> ll = new ArrayList<>();
      if (!CollectionUtils.isEmpty(test)) {
          test.forEach(i -> {
              LHQuestionnaireVo vo = new LHQuestionnaireVo();
              BeanUtils.copyProperties(i, vo);
              vo.setId(i.getId().toHexString());
              LHlock isLock = soulServiceApi.findIsLock(UserHolder.getUserId(), i.getLevel());
              if (isLock!=null){
                  vo.setIsLock(isLock.getLock());
                  vo.setLevel(QuestionnaireLeverEnum.getLever(isLock.getLevel()).toString());
              }else{
                  vo.setLevel(QuestionnaireLeverEnum.LOW.toString());
              }
              ObjectId id = i.getId();
              List<LHQuestion> question = soulServiceApi.findQuestion(id.toHexString());
              List<QuestionDto> l = new ArrayList<>();
              question.forEach(a -> {
                  QuestionDto questionDto = new QuestionDto();
                  BeanUtils.copyProperties(a, questionDto);
                  questionDto.setId(a.getId().toHexString());
                  List<LHOptions> option = soulServiceApi.findOption(a.getId().toHexString());
                  List<OptionsDto> list = new ArrayList<>();
                  option.forEach(b -> {
                      OptionsDto optionsDto = new OptionsDto();
                      BeanUtils.copyProperties(b, optionsDto);
                      optionsDto.setId(b.getId().toHexString());
                      list.add(optionsDto);
                  });
                  questionDto.setOptions(list);
                  l.add(questionDto);
              });
              vo.setQuestions(l);
              boolean reportId = soulServiceApi.findReportId(UserHolder.getUserId());
              if (reportId) {
                  vo.setReportId(1 + "");
              }

              ll.add(vo);
          });
      }
      return ll;
  }
  }
